package work;
/*
9 编程 有一个按升序排好序的数组,现输入一个数,要求按原来的规律把它插入数组中
 */
public class Work09 {

    public static void main(String[] args) {
        int[] sortedArray = {1, 3, 5, 7, 9};
        int numberToInsert = 6;

        int[] resultArray = insertNumber(sortedArray, numberToInsert);

        for (int i : resultArray) {
            System.out.print(i + " ");
        }
    }
    static int[] insertNumber(int[] sortedArray, int number) {
        int[] newArray = new int[sortedArray.length + 1];
        int index = 0;

        for (int i = 0; i < sortedArray.length; i++) {
            if (sortedArray[i] > number) {
                break;
            }
            newArray[index++] = sortedArray[i];
        }

        newArray[index] = number;

        for (int i = index; i < sortedArray.length; i++) {
            newArray[index + 1] = sortedArray[i];
            index++;
        }

        return newArray;
    }

}